import MyPlugin from "../../main";
import {MarkdownView, TFile} from "obsidian";
import moment from "moment";
import path from "path";


export async function createNewFileByData(this_:MyPlugin,name:string){
	// 获取当前日期
	const currentDate = moment();
	// 创建年、月、周文件夹
	const yearFolder = currentDate.format('YYYY年');
	const monthFolder = currentDate.format('MM月');
	const dayFolder = currentDate.format('D日');
	const formatString = currentDate.format('YYYY年MM月D日HH时ss秒');
	let temPathPre = "";
	if (!this_.settings.sjzNewFilePath){
		temPathPre="日历";
	}
	const filePath = temPathPre+path.sep+yearFolder+path.sep+monthFolder+path.sep+dayFolder+path.sep+name+formatString+".md";
	console.log(filePath)
	this_.app.vault.adapter.exists(temPathPre).then((exists) => {
		if (!exists){
			this_.app.vault.adapter.mkdir(temPathPre)
		}
	})
	this_.app.vault.adapter.exists(temPathPre+path.sep+yearFolder).then((exists) => {
		if (!exists){
			this_.app.vault.adapter.mkdir(temPathPre+path.sep+yearFolder)
		}
	})
	this_.app.vault.adapter.exists(temPathPre+path.sep+yearFolder+path.sep+monthFolder).then((exists) => {
		if (!exists){
			this_.app.vault.adapter.mkdir(temPathPre+path.sep+yearFolder+path.sep+monthFolder)
		}
	})
	this_.app.vault.adapter.exists(temPathPre+path.sep+yearFolder+path.sep+monthFolder+path.sep+dayFolder).then((exists) => {
		if (!exists){
			this_.app.vault.adapter.mkdir(temPathPre+path.sep+yearFolder+path.sep+monthFolder+path.sep+dayFolder)
		}
	})
	this_.app.vault.adapter.exists(filePath).then((exists) => {
		if (!exists){
			this_.app.vault.adapter.write(filePath,"")
		}
	})
	判断文件是否激活并打开(filePath,this_)
}

export async function createNewFile(this_:MyPlugin,filePath1:string){
	await this_.app.vault.create(filePath1, "");
	await this_.app.workspace.openLinkText(filePath1, '/', true);
}

export async function 保存临时数据到文件(this_: MyPlugin, filePath: string, data: Record<string, number>, path: string) {
	// 每次打开文件时候将当前打开的文件保存到最近文件中
	data[filePath] = new Date().getTime();
	const temPath = this_.app.vault.getAbstractFileByPath(path);
	if (temPath instanceof TFile) {
		try {
			await this_.app.vault.modify(temPath, JSON.stringify(data));
			console.log('文件保存成功');
		} catch (error) {
			console.error('文件保存失败:', error);
		}
	} else {
		console.error('路径无效或文件不存在');
	}
}

export async function 判断文件是否激活并打开(filePath: string, this_:MyPlugin) {
	let foundLeaf = null;
	// 检查文件是否已经打开
	this_.app.workspace.getLeavesOfType('markdown').forEach(leaf => {
		const markdownView = leaf.view as MarkdownView; // 这里进行类型断言
		if (markdownView.file && markdownView.file.path === filePath) {
			foundLeaf = leaf;
		}
	});
	if (foundLeaf) {
		// 如果文件已经打开，激活对应的标签
		this_.app.workspace.setActiveLeaf(foundLeaf, true, true);
	} else {
		this_.app.workspace.openLinkText(filePath, '/', true);
	}
}

export async function 在当前页面打开文件(filePath: string, this_:MyPlugin) {
	// 确保 app 和 vault 不是 null 或 undefined
	if (this_.app && this_.app.vault) {
		const file = this_.app.vault.getAbstractFileByPath(filePath);
		if (file instanceof TFile) {
			// 确保 activeLeaf 不是 null 或 undefined
			if (this_.app.workspace.activeLeaf) {
				await this_.app.workspace.activeLeaf.openFile(file);
			}
		}
	}
}

// 加锁要注意,打开新标签页和正常的页面是不一样的
// @ts-ignore
export function  redisLocak(key: string,this_:MyPlugin): boolean{
	if (null==this_.redisLock[key] || new Date().getTime()-this_.redisLock[key]>1){
		this_.redisLock[key]=new Date().getTime()
		return true;
	}else {
		return false;
	}
}
